<?php defined('SYSPATH') OR die('No direct access allowed.');

class json_Core {

    /**
     * Read JSON file
     * @param string $path File path
     * @param array $array Return as array
     * @return Array if $array is TRUE else $object
     */
    public static function readfile($path, $array = TRUE)
    {
        $file_content = file_get_contents($path);
        return json_decode($file_content , $array);
    }

    /**
     * Write JSON file
     * @param object $object Object to be written
     * @param string $filepath File path
     */
    public static function writefile($object, $filepath, $indent = FALSE)
    {
        $content = json_encode($object);
        if ($indent)
        {
            $content = self::indent($content);
        }
        return file_put_contents($filepath, $content);
    }

    /**
     * Indent JSON to be readable
     * Taken from http://code.google.com/p/gdata-samples/source/browse/trunk/hybrid/common.inc.php
     * @param string $json
     * @return indented JSON
     */
    public static function indent($json)
    {
        $tab = '  ';
        $new_json = '';
        $indent_level = 0;
        $in_string = false;
        $json_obj = json_decode($json);

        if(!$json_obj)
        {
            return $new_json;
        }

        $json = json_encode($json_obj);
        $len = strlen($json);

        for($c = 0; $c < $len; $c++)
        {
            $char = $json[$c];
            switch($char)
            {
                case '{':
                    if(!$in_string)
                    {
                        $new_json .= $char . "\n" . str_repeat($tab, $indent_level + 1);
                        $indent_level++;
                    }
                    else
                    {
                        $new_json .= $char;
                    }
                    break;
                case '[':
                    if(!$in_string)
                    {
                        $new_json .= $char . "\n" . str_repeat($tab, $indent_level + 1);
                        $indent_level++;
                    }
                    else
                    {
                        $new_json .= $char;
                    }
                    break;
                case '}':
                    if(!$in_string)
                    {
                        $indent_level--;
                        $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
                    }
                    else
                    {
                        $new_json .= $char;
                    }
                    break;
                case ']':
                    if(!$in_string)
                    {
                        $indent_level--;
                        $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
                    }
                    else
                    {
                        $new_json .= $char;
                    }
                    break;
                case ',':
                    if(!$in_string)
                    {
                        $new_json .= ",\n" . str_repeat($tab, $indent_level);
                    }
                    else
                    {
                        $new_json .= $char;
                    }
                    break;
                case ':':
                    if(!$in_string)
                    {
                        $new_json .= ': ';
                    }
                    else
                    {
                        $new_json .= $char;
                    }
                    break;
                case '"':
                    $in_string = !$in_string;
                    $new_json .= $char;
                    break;
                default:
                    $new_json .= $char;
                    break;
            }
        }
        return $new_json;
    }
}